<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="资产编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['propertyNo', validatorRules.propertyNo]" placeholder="请输入资产编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="资产类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['type', validatorRules.type]" :trigger-change="true" dictCode="property_type" placeholder="请选择资产类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['propertyName', validatorRules.propertyName]" placeholder="请输入名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="挂牌价格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :min="0" :formatter="value => `${value}万元`"  v-decorator="['listingPrice', validatorRules.listingPrice]" placeholder="请输入挂牌价格" style="width: 100%"/>
            </a-form-item>
          </a-col>


          <a-col :span="12">
            <a-form-item label="保证金" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number  :min="0" :formatter="value => `${value}元`" :parser="value => value.replace('元', '')"  v-decorator="['earnestMoney', validatorRules.earnestMoney]" placeholder="请输入保证金" style="width: 100%" />
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="地区" :labelCol="labelCol" :wrapperCol="wrapperCol" required >
<!--              <JAreaLinkagePca  @change="areaChange"  v-decorator="['city', validatorRules.city]"></JAreaLinkagePca>-->
              <JAreaLinkagePca  @change="areaChange"  :value="city"></JAreaLinkagePca>
              <div class="ant-form-explain haserror1 " v-if="diquShow">请选择地区!</div>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="轮播图" :labelCol="{span:2}" :wrapperCol="{span:21}">
              <j-image-upload isMultiple v-decorator="['urls', validatorRules.urls]" ></j-image-upload>
            </a-form-item>
          </a-col>
          <!--<a-col :span="12">
            <a-form-item label="报名开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择报名开始时间" v-decorator="['enrollStartTime', validatorRules.enrollStartTime]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="报名结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择报名结束时间" v-decorator="['enrollEndTime', validatorRules.enrollEndTime]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-item label="交易须知" :labelCol="{span:2}" :wrapperCol="{span:21}" required>
              <j-editor v-decorator="['tradingInstruction',{trigger:'input'}]"/>
              <div class="ant-form-explain haserror " v-if="jiaoyixuzhiShow">请填写交易须知!</div>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="资产详情" :labelCol="{span:2}" :wrapperCol="{span:21}" required>
              <j-editor v-decorator="['propertyDetail', {trigger:'input'}]" />
              <div class=" ant-form-explain haserror" v-if="zichanxiangqingShow">请填写资产详情!</div>
            </a-form-item>
          </a-col>

          <!--<a-col :span="24">
            <a-form-item label="省" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['province', validatorRules.province]" placeholder="请输入省"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="市" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['city', validatorRules.city]" placeholder="请输入市"></a-input>
            </a-form-item>
          </a-col>-->

          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>

  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JEditor from '@/components/jeecg/JEditor'
  import JAreaLinkagePca from '@/components/jeecg/JAreaLinkagePca'
  import JImageUpload from '@/components/jeecg/JImageUpload'

  export default {
    name: 'PropertyForm',
    components: {
      JFormContainer,
      JDate,
      JDictSelectTag,
      JEditor,
      JAreaLinkagePca,
      JImageUpload
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        jiaoyixuzhiShow:false,
        zichanxiangqingShow:false,
        diquShow:false,
        model: {},
        city:'',
        province:'',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          propertyNo: {
            rules: [
              { required: true, message: '请输入资产编号!'},
            ]
          },
          tradingInstruction:{
            rules: [
              { required: true, message: '请填写交易须知!'},
            ]
          },
          propertyName: {
            rules: [
              { required: true, message: '请输入名称!'},
            ]
          },
          type:{
            rules: [
              { required: true, message: '请选择资产类型!'},
            ]
          },
          propertyDetail: {
            rules: [
              { required: true, message: '请输入资产详情!'},

            ]
          },
          earnestMoney:{
            rules: [
              { required: true, message: '请输入保证金!'},
            ]
          },
          listingPrice: {
            rules: [
              { required: true, message: '请输入挂牌价格!'},
              {
                validator: (rule, value, callback) => {
                  var reg = new RegExp("^[0-9]*$");
                  if (!reg.test(value)){
                    return false
                  }else {
                    return true
                  }
                  // return this.$u.test.mobile(value);
                },
                message: '挂牌价格请输入数字',
                // 触发器可以同时用blur和change
                trigger: ['change','blur'],
              }

            ]
          },
          province: {
            rules: [
              { required: true, message: '请输入省!'},
            ]
          },
          city: {
            rules: [
              { required: true, message: '请选择地区!'},
            ]
          },
          enrollStartTime: {
            rules: [
              { required: true, message: '请输入报名开始时间!'},
            ]
          },
          enrollEndTime: {
            rules: [
              { required: true, message: '请输入报名结束时间!'},
            ]
          },
          urls: {
            rules: [
              { required: true, message: '请上传资产轮播图!'},
            ]
          },
        },
        url: {
          add: "/property/add",
          edit: "/property/edit",
          queryById: "/property/queryById"
        },
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        console.log('record',record)
        this.city = record.city
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'propertyNo','type','propertyName','propertyDetail','listingPrice','earnestMoney','tradingInstruction','enrollStartTime','enrollEndTime','urls','city'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        if (this.city){
          this.diquShow = false
        }else {
          this.diquShow = true
        }
        // 触发表单验证
        this.form.validateFields((err, values) => {
          console.log(values)
          if (values.propertyDetail){
            this.zichanxiangqingShow = false
          }else {
            this.zichanxiangqingShow = true
          }
          if (values.tradingInstruction){
            this.jiaoyixuzhiShow = false
          }else {
            this.jiaoyixuzhiShow = true
          }
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values,{province:this.province,city:this.city});
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'propertyNo','type','propertyName','propertyDetail','listingPrice','earnestMoney','tradingInstruction','enrollStartTime','enrollEndTime','urls'))
      },

      //省市联动
      areaChange(e){
        // console.log(e)
        if (this.city){
          this.diquShow = false
        }else {
          this.diquShow = true
        }
        this.province = e[0]
        this.city = e[1]
        // this.model.city = e[1]
      }
    }
  }
</script>
<style>
  .haserror{
    color: #f5222d;
    margin-top: 5px;
  }
  .haserror1{
    color: #f5222d;
  }
</style>